import {
    Command
} from '../command/command'
//新增节点的命令
class AddNodeCommand extends Command {
    constructor(node, parent) {
        super('addNode');
        this.mind = parent.getMinder();
        this.node = node;
        this.parent = parent;
    }
    execute() {
        this.mind.addNode(this.node, this.parent);
        this.mind.refresh();
        this.node.refresh();
        this.parent.refresh();
    }
    undo() {
        this.mind.removeNode(this.node);
        this.mind.refresh();
        this.node.refresh();
        this.parent.refresh();
    }
}
//移除节点的命令
class RemoveNodeCommand extends Command {
    constructor(node, parent) {
        super('removeNode');
        this.mind = node.getMinder();
        this.node = node;
        this.parent = parent;
    }
    execute() {
        this.index = this.mind.removeNode(this.node);
        this.mind.refresh();
        this.node.refresh();
        this.parent.refresh();
    }
    undo() {
        this.mind.addNode(this.node, this.parent, this.index);

        this.mind.traverseBF((n) => {
            this.mind.add(n);
            if (n.induce) {
                this.mind.addInduce(n.induce);
            }
        }, this.node);
        this.mind.refresh();

        this.node.refresh();
        this.parent.refresh();
    }
}
//为了简便,这里统一实现一个更改Node的命令
class ChangeNodeCommand extends Command {
    constructor(node, oldData, newData) {
        super('changeNode');
        this.node = node;
        this.oldData = oldData; //防止出现引用的问题
        this.newData = newData;
    }
    execute() {
        this.node.setData(this.newData);
        this.node.getMinder().refresh();
    }
    undo() {
        this.node.setData(this.oldData);
        this.node.getMinder().refresh();
    }
}

//新增归纳的命令
class AddInduceCommand extends Command {
    constructor(node, induce) {
        super('addInduce');
        this.node = node;
        this.induce = induce;
    }
    execute() {
        this.node.getMinder().addInduce(this.induce);
        this.node.induce = this.induce;
        this.node.getMinder().refresh();
    }
    undo() {
        this.node.getMinder().removeInduce(this.induce);
        this.node.induce = null;
        this.node.getMinder().refresh();
    }
}
class RemoveInduceCommand extends Command {
    constructor(node, induce) {
        super('removeCommand')
        this.node = node;
        this.induce = induce;
    }
    execute() {
        this.node.getMinder().removeInduce(this.induce);
        this.node.induce = null;
        this.node.getMinder().refresh();
    }
    undo() {
        this.node.getMinder().addInduce(this.induce);
        this.node.induce = this.induce;
        this.node.getMinder().refresh();
    }
}
class ChangeInduceCommand extends Command {
    constructor(induce, oldValue, newValue) {
        super('changeInduceCommand')
        this.induce = induce;
        this.oldValue = oldValue;
        this.value = newValue;
    }
    execute() {
        this.induce.setText(this.value);
        this.induce.refresh();
        this.induce.node.getMinder().refresh();
    }
    undo() {
        this.induce.setText(this.oldValue);
        this.induce.refresh();
        this.induce.node.getMinder().refresh();
    }
}

//新增线框的命令
class AddWireFrameCommand extends Command{
    constructor(node,wf){
        super();
        this.node=node;
        this.wireFrame=wf;
        this.node.wireFrame=wf;
    }
    execute(){
        this.node.getMinder().addWireFrame(this.wireFrame);
    }
    undo(){
        this.node.getMinder().removeWireFrame(this.wireFrame);
        this.node.wireFrame=null;
    }
}

//移除线框的命令
class RemoveWireFrameCommand extends Command{
    constructor(node,wf){
        super();
        this.node=node;
        this.wireFrame=wf;
        this.node.wireFrame=wf;
    }
    execute(){
        this.node.getMinder().removeWireFrame(this.wireFrame);
        this.node.wireFrame=null;
    }
    undo(){
        this.node.getMinder().addWireFrame(this.wireFrame);
    }
}


export {
    AddNodeCommand,
    RemoveNodeCommand,
    ChangeNodeCommand,
    AddInduceCommand,
    RemoveInduceCommand,
    ChangeInduceCommand,
    AddWireFrameCommand,
    RemoveWireFrameCommand
}